package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.Adapter.FastBuyAdapter;
import com.mmlc.meimei.lichengkongjian.Adapter.MyLoveAdapter;
import com.mmlc.meimei.lichengkongjian.MainActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.classify.FastGoodsDetailActivity;
import com.mmlc.meimei.lichengkongjian.classify.ProductInfoActivity;
import com.mmlc.meimei.lichengkongjian.customview.SwipeRecyclerView;
import com.mmlc.network.filter.MyLoveRequestFilter;
import com.mmlc.network.filter.WoyaoshiRequestFilter;
import com.mmlc.network.responsebean.GoodsDetailResponseBean;
import com.mmlc.network.responsebean.MyLoveResponseBean;
import com.mmlc.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ActivityMyLove extends InterActivity {


    @InjectView(R.id.tv_title)
    TextView tvTtitle;
    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.yijianshiyi)
    Button yijianshiyi;
    @InjectView(R.id.swipeRecyclerView)
    SwipeRecyclerView mSwipeRecyclerView;

    boolean isRefresh = false,isLoadMore = false,isFirst = true;
    int page = 1;
    private MyLoveAdapter adapter;

    @Override
    protected int initContentView() {
        return R.layout.activity_mylove;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        processData();
    }

    List<MyLoveResponseBean.DataBean> data;
    private void processData() {
        //处理TitleBar
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvTtitle.setText("我喜欢的");

        yijianshiyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(yijianshiyi.getText().toString().equals("一键试衣")){
                    yijianshiyi();
                    yijianshiyi.setText("确定");
                }else if(yijianshiyi.getText().toString().equals("确定")){
                    yijianshiyi.setText("一键试衣");
                    save();
                }

            }
        });
        mSwipeRecyclerView.getSwipeRefreshLayout().setColorSchemeColors(Color.RED);
        mSwipeRecyclerView.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        data = new ArrayList<>();
        adapter = new MyLoveAdapter(this,data);
        mSwipeRecyclerView.setAdapter(adapter);
        mSwipeRecyclerView.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                isLoadMore = false;
                page = 1;
                initData(page);
            }

            @Override
            public void onLoadMore() {
                page++;
                isLoadMore = true;
                isRefresh = false;
                initData(page);
            }
        });
        adapter.setClickListener(new FastBuyAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                if(data.get(position).getIs_zg()==1){
                    Intent intent = new Intent(ActivityMyLove.this, FastGoodsDetailActivity.class);
                    intent.putExtra("id", data.get(position).getId());
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(ActivityMyLove.this, ProductInfoActivity.class);
                    intent.putExtra("goods_id", data.get(position).getId());
                    startActivity(intent);
                }

            }
        });
        initData(1);
    }


    public int pageNow = 1;
    public int pageSize = 7;
    MyLoveRequestFilter orderListFilter;
    boolean exe1 = true;
    private void initData(int page) {

        if(orderListFilter == null){
            orderListFilter = new MyLoveRequestFilter(ActivityMyLove.this);
            orderListFilter.requestBean.method = "myCollect";
        }
            orderListFilter.requestBean.pageNow = page;
        if(exe1){
            exe1 = false;
            orderListFilter.isTransparence = false;
        }else{
            orderListFilter.isTransparence = true;
        }
        orderListFilter.requestBean.pageSize = pageSize;
        orderListFilter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<MyLoveResponseBean>() {

            @Override
            public void onSuccess(MyLoveResponseBean result) {
                if(result!=null){
                    if(isFirst){
                        data.addAll(result.getData());
                        adapter.notifyDataSetChanged();
                        isFirst = false;
                        if(result.getData().size()<pageSize){
                            mSwipeRecyclerView.setLoadMoreEnable(false);
                        }
                        mSwipeRecyclerView.complete();
                        return;
                    }
                    if(isLoadMore ) {
                        data.addAll(result.getData());
                        isLoadMore = false;
                        if(result.getData().size()==0){
                            mSwipeRecyclerView.onNoMore("一  End  一");
                        }else{
                            mSwipeRecyclerView.stopLoadingMore();
                            adapter.notifyDataSetChanged();
                        }
                        adapter.notifyDataSetChanged();
                        return;
                    }else if(isRefresh){
                        if(result.getData().size()<pageSize){
                            mSwipeRecyclerView.setLoadMoreEnable(false);
                        }
                        if(result.getData().size() == 0){
                            mSwipeRecyclerView.complete();
                            isRefresh = false;
                            return;
                        }

                        data.clear();
                        data.addAll(result.getData());
                        isRefresh = false;
                        mSwipeRecyclerView.complete();
                        adapter.notifyDataSetChanged();
                        return;
                    }
                }else{
                    ToastUtils.showToast("数据类型错误");
                }

            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
                mSwipeRecyclerView.complete();
                mSwipeRecyclerView.stopLoadingMore();
            }
        });
    }

    void yijianshiyi(){
        adapter.setShowCheck(true);
        adapter.notifyDataSetChanged();
    }

    void save(){
        for (MyLoveResponseBean.DataBean dataBean:data){
            if(dataBean.isCheck()){
                WoyaoshiRequestFilter woyaoshiRequestFilter=new WoyaoshiRequestFilter(this);
                woyaoshiRequestFilter.requestBean.goods_id = dataBean.getId();
                woyaoshiRequestFilter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GoodsDetailResponseBean>() {
                    @Override
                    public void onSuccess(GoodsDetailResponseBean result) {
                        ToastUtils.showToast(result.getMessage());
                        adapter.setShowCheck(false);
                        adapter.notifyDataSetChanged();
                        Intent intent =new Intent(ActivityMyLove.this,MainActivity.class);
                        intent.putExtra("flag", 4);
                        startActivity(intent);
                    }

                    @Override
                    public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                        ToastUtils.showToast(serverError.mess);
                    }
                });
            }
        }
    }


}
